﻿/*----------------------------------------------------------------
// Copyright (C) 2016 通通优品版权所有。
// 命名空间: TongTongMall.MallWeb.Core.Components.Services
// 类名：WXCacheDomainService
// 功能描述：N/A
// 
// 创建标识：Roc.Lee(李鹏鹏) 2017/8/14 18:45:27 TEC-ROCLEE Roc.Lee

// 修改标识：
// 修改描述：
// 
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TEC.Public.Component.OpenPlat.Tencent.WeiXin;
using TongTongMall.Dependecy;
using TongTongMall.MallWeb.Core.Instance.Config;
using TongTongMall.MallWeb.Core.Pay.Services;

namespace TongTongMall.MallWeb.Core.Components.Services
{
    public class WXCacheDomainService : TongTongMallDomainServiceBase, IWXCacheDomainService
    {
        private readonly IPayDomainService _IPayDomainService;
        private readonly IPublicCacheService _IPublicCacheService;

        public WXCacheDomainService(IPayDomainService _IPayDomainService)
        {
            this._IPayDomainService = _IPayDomainService;
            _IPublicCacheService = IocManager.Instance.Resolve<PublicRedisManager>();

        }

        public async Task<AccessToken> GetWX_AccessToken(BaseConfig config)
        {
            var accesstoken = await _IPublicCacheService.GetWX_AccessToken<AccessToken>(config.APPID);
            if (accesstoken == null || accesstoken.IsExpires())
            {
                accesstoken = AccessToken.GetAccessTokenInfoFromService(config.APPID, config.APPSECRET);
                await _IPublicCacheService.SetWX_AccessToken(accesstoken, TimeSpan.FromSeconds(accesstoken.expires_in), config.APPID);

            }
            return accesstoken;
        }

        public async Task<AccessToken> GetWX_AccessToken(long? tenantId)
        {
            var config = await _IPayDomainService.GetConfig<WapWXPayConfig>(tenantId.HasValue ? tenantId.Value : 0) as BaseConfig;
            return await GetWX_AccessToken(config);
        }

        public async Task<JsApiTicket> GetWX_JsApiTicket(BaseConfig config)
        {
            var jsapiticket = await _IPublicCacheService.GetWX_JsApiTicket<JsApiTicket>(config.APPID);
            if (jsapiticket == null || jsapiticket.IsExpires())
            {
                jsapiticket = JsApiTicket.GetJsApiTicketFromService((await GetWX_AccessToken(config)).access_token);
                await _IPublicCacheService.SetWX_JsApiTicket(jsapiticket, TimeSpan.FromSeconds(jsapiticket.expires_in), config.APPID);
            }
            return jsapiticket;
        }

        public async Task<JsApiTicket> GetWX_JsApiTicket(long? tenantId)
        {
            var config = await _IPayDomainService.GetConfig<WapWXPayConfig>(tenantId.HasValue ? tenantId.Value : 0) as BaseConfig;
            return await GetWX_JsApiTicket(config);
        }

        public async Task UpdateWX_AccessToken(BaseConfig config)
        {
            var accesstoken = AccessToken.GetAccessTokenInfoFromService(config.APPID, config.APPSECRET);
            await _IPublicCacheService.SetWX_AccessToken(accesstoken, TimeSpan.FromSeconds(accesstoken.expires_in), config.APPID);
        }

        public async Task UpdateWX_AccessToken(long? tenantId)
        {
            var config = await _IPayDomainService.GetConfig<WapWXPayConfig>(tenantId.HasValue ? tenantId.Value : 0) as BaseConfig;
            await UpdateWX_AccessToken(config);
        }

        public async Task UpdateWX_JsApiTicket(BaseConfig config)
        {
            var jsapiticket = JsApiTicket.GetJsApiTicketFromService((await GetWX_AccessToken(config)).access_token);
            await _IPublicCacheService.SetWX_JsApiTicket(jsapiticket, TimeSpan.FromSeconds(jsapiticket.expires_in), config.APPID);
        }

        public async Task UpdateWX_JsApiTicket(long? tenantId)
        {
            var config = await _IPayDomainService.GetConfig<WapWXPayConfig>(tenantId.HasValue ? tenantId.Value : 0) as BaseConfig;
            await UpdateWX_JsApiTicket(config);
        }
    }
}
